//
//  LMSelecteButton.m
//  Perking
//
//  Created by longmin on 16/9/8.
//  Copyright © 2016年 tim. All rights reserved.
//

#import "LMSelecteButton.h"

#define kIconW 14
#define kImageW 20
#define kPadding 8

@interface LMSelecteButton()

@property(nonatomic, strong) UIImageView *iconView;//选中图标

@property(nonatomic, strong) UIImageView *imageView;//选项的图标

@property(nonatomic, strong) UILabel *titleLabel;
/** 点击的回调 */
@property(nonatomic, strong) void(^clickHandler)(BOOL isSelect);

@end

@implementation LMSelecteButton

#pragma mark - 生命周期

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self addSubview:self.iconView];
        [self addSubview:self.imageView];
        [self addSubview:self.titleLabel];
        self.select = NO;
    }
    return self;
}
#pragma mark - set方法和get方法

-(void)setSelect:(BOOL)select {
    _select = select;
    if (select) {
        self.iconView.image = [UIImage imageNamed:@"icon_select_yes"];
    }else {
        self.iconView.image = [UIImage imageNamed:@"icon_select_no"];
    }
}

-(UIImageView *)iconView {
    if (_iconView == nil) {
        _iconView = [[UIImageView alloc] init];
    }
    return _iconView;
}

-(UILabel *)titleLabel {
    if (_titleLabel == nil) {
        _titleLabel = [[UILabel alloc] init];
    }
    return _titleLabel;
}

- (UIImageView *)imageView {
    if (_imageView == nil) {
        _imageView = [[UIImageView alloc] init];
    }
    return _imageView;
}

#pragma mark - IBActions

/** 点击 */
-(void)tapClick {
    
    if (self.clickHandler) {
        
        self.select = !self.select;
        
        self.clickHandler(self.isSelect);
    }
    
}

#pragma mark - Public

+(instancetype)selectButtonWithFont:(UIFont *)font title:(NSString *)title color:(UIColor *)color clickHandler:(void (^)(BOOL))clickHandler{
    LMSelecteButton *selectBtn = [[LMSelecteButton alloc] init];
    selectBtn.titleLabel.font = font;
    selectBtn.titleLabel.text = title;
    selectBtn.titleLabel.textColor = color;
    selectBtn.clickHandler = clickHandler;
    
    CGFloat titleW = [title boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, kIconW)
                                         options:NSStringDrawingUsesLineFragmentOrigin
                                      attributes:@{NSFontAttributeName : font}
                                         context:nil].size.width;
    
    selectBtn.width = kIconW + kPadding + titleW;
    selectBtn.height = kIconW;
    
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:selectBtn action:@selector(tapClick)];
    [selectBtn addGestureRecognizer:tapGesture];
    
    return selectBtn;
}

+(instancetype)selectButtonWithFont:(UIFont *)font
                              title:(NSString *)title
                              color:(UIColor *)color
                              image:(NSString *)itemImage
                       clickHandler:(void (^)(BOOL))clickHandler {
    
    LMSelecteButton *selectBtn = [self selectButtonWithFont:font title:title color:color clickHandler:clickHandler];
    
    if (itemImage.length) {
        selectBtn.imageView.image = [UIImage imageNamed:itemImage];
    }
    
    if (selectBtn.imageView.image) {
        selectBtn.width += kImageW;
    }
    
    return selectBtn;
}

- (void)setTitle:(NSString *)title {
    self.titleLabel.text = title;
    [self setNeedsLayout];
}

#pragma mark - Private
#pragma mark - 数据源和代理方法
#pragma mark - UIView -> 下面放继承自UIView的方法

-(void)layoutSubviews {
    
    //选择图标
    self.iconView.left = 0;
    self.iconView.width = self.iconView.height = kIconW;
    self.iconView.centerY = self.height * 0.5;
    
    //文字
    self.titleLabel.size = [self.titleLabel.text boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, kIconW)
                                               options:NSStringDrawingUsesLineFragmentOrigin
                                            attributes:@{NSFontAttributeName : self.titleLabel.font}
                                               context:nil].size;
    self.titleLabel.left = self.iconView.right + kPadding;
    self.titleLabel.centerY = self.iconView.centerY;
    
    //图片
    if (self.imageView.image) {
        self.imageView.width = self.imageView.height = kImageW;
        self.imageView.centerY = self.iconView.centerY;
        self.imageView.left = self.iconView.right + kPadding;
        self.titleLabel.left = self.imageView.right + kPadding;
    }
    
}

@end
